﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FRL.DebtChased.Extensions;

namespace FRL.DebtChased.Repository.ViewModels
{

	/// <summary>
	/// Declaration of WorkflowListModel class
	/// </summary>
	public class WorkflowListModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			internal int m_iWorkflowId = 0;
			public int WorkflowId
			{
				get { return m_iWorkflowId; }
			}

			public string Name { get; set; }

			public string Notes { get; set; }

			public string ModifiedAsText { get; set; }

			public int UsageCount { get; set; }

			public string InUseAsText { get; set; }

		}

		/////////////////////////////////////////////////////////////
		// Constants and Enumerations

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public WorkflowListModel(DebtChasedEntities ent)
		{

			// Load from workflow table
			var results = ent.WorkflowProc();

			// Load the element list
			foreach (var ob in results)
			{

				// Add an element
				Element el = new Element();
				el.m_iWorkflowId = ob.fdWorkflowId;
				el.UsageCount = ob.fdUsageCount;
				el.InUseAsText = ob.fdUsageCount>0?"Yes":"No";
				el.Name = ob.fdName;
				el.Notes = ob.fdNotes;
				m_elements.Add(el);

				// If there is a modification time...
				if(ob.fdModifiedAt != null)
					el.ModifiedAsText = string.Format("{0} by {1}",
						ob.fdModifiedAt.Value.ToCuteString(), ob.fdModifiedBy
					);

			}

		}

	}

}
